package com.whq.knowverywell.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.whq.knowverywell.R;
import com.whq.knowverywell.business.model.ProductSalesRecord;
import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.StringUtil;

public class ProductSearchAdapter extends BaseAdapter {
	private static final String TAG = ProductSearchAdapter.class.getSimpleName(); 
	
//	private Activity ctx;
	private List<ProductSalesRecord> list;
	private LayoutInflater inflater;
	
	public ProductSearchAdapter(Activity ctx, List<ProductSalesRecord> list) {
//		this.ctx = ctx;
		this.list = list;
		this.inflater = LayoutInflater.from(ctx);
		if (this.list == null) {
		    this.list = new ArrayList<ProductSalesRecord>();
		}
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public ProductSalesRecord getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LogUtil.d(TAG, "position---------[" + position + "]");
		
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.product_search_item, null);
			viewHolder = new ViewHolder();
			viewHolder.productNoLabel = (TextView) convertView.findViewById(R.id.product_no_label);
			viewHolder.productNoTextView = (TextView) convertView.findViewById(R.id.product_no_textview);
			viewHolder.tagPriceTextView = (TextView) convertView.findViewById(R.id.tag_price_textview);
			viewHolder.surplusStockTextView = (TextView) convertView.findViewById(R.id.surplus_stock_textview);
			viewHolder.accumulativeSellCountTextView = (TextView) convertView.findViewById(R.id.accumulative_sell_count_textview);
			viewHolder.averageDiscountTextview = (TextView) convertView.findViewById(R.id.average_discount_textview);
			viewHolder.accumulativeSellAmountTextview = (TextView) convertView.findViewById(R.id.accumulative_sell_amount_textview);
			viewHolder.productImageView = (ImageView) convertView.findViewById(R.id.product_imageview);
			viewHolder.titleRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.title_relativelayout);
			
			viewHolder.tvColorCode = (TextView) convertView.findViewById(R.id.tv_color_code);
			viewHolder.tvSizeCode = (TextView) convertView.findViewById(R.id.tv_size_code);
			
			convertView.setTag(viewHolder);
		} else {
		    viewHolder = (ViewHolder) convertView.getTag();
		}
		
		ProductSalesRecord record = list.get(position);
		if (record == null) {
		    return convertView;
		}
		
		if (position == 0) {
			viewHolder.titleRelativeLayout.setVisibility(View.VISIBLE);
		    viewHolder.productNoLabel.setText("货号");
		    viewHolder.titleRelativeLayout.setBackgroundResource(R.drawable.title_page_red_bg);
		    viewHolder.productNoTextView.setText(setValueInElement(record.getCode(), ""));
		} else if (position == 1) {
			viewHolder.titleRelativeLayout.setVisibility(View.VISIBLE);
		    viewHolder.productNoLabel.setText("款号");
		    viewHolder.titleRelativeLayout.setBackgroundResource(R.drawable.title_page_blue_bg);
		    viewHolder.productNoTextView.setText(setValueInElement(record.getPropertyCode(), ""));
		} else {
			viewHolder.titleRelativeLayout.setVisibility(View.GONE);
		}
		
		viewHolder.tvColorCode.setText(setValueInElement(record.getColorCode(), ""));
		viewHolder.tvSizeCode.setText(setValueInElement(record.getSizeCode(), ""));
		
		viewHolder.tagPriceTextView.setText("￥ " + setValueInElement(record.getPriceTag() + "", "0.00"));
		viewHolder.surplusStockTextView.setText(setValueInElement(record.getStockQty() + "", ""));
		viewHolder.accumulativeSellCountTextView.setText(setValueInElement(record.getSalesVolume() + "", ""));
		viewHolder.averageDiscountTextview.setText(setValueInElement(record.getAverageDiscount() + "", ""));
		viewHolder.accumulativeSellAmountTextview.setText("￥ " + setValueInElement(record.getSalesAmount() + "", "0.00"));
		AQuery aq = new AQuery(convertView);
		aq.id(viewHolder.productImageView).image(record.getImageUrl(), true, true, 90, 0, null, AQuery.FADE_IN, 1.0f);
		
		return convertView;
	}
	
	class ViewHolder {
	    TextView productNoLabel;
		TextView productNoTextView;
		TextView tagPriceTextView;
		TextView surplusStockTextView;
		TextView accumulativeSellCountTextView;
		TextView averageDiscountTextview;
		TextView accumulativeSellAmountTextview;
		RelativeLayout titleRelativeLayout;
		ImageView productImageView;
		
		TextView tvColorCode;
		TextView tvSizeCode;
	}
	
	private String setValueInElement(String value, String defaultValue) {
	    if (StringUtil.isEmpty(value)) {
	        return defaultValue;
	    } else {
	        return value;
	    }
	}

}
